<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.id?$t('编辑') : $t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="485px" top="20vh" @close='close'>
    <div class="dialogContentBoxMaxHeight60">
      <el-form ref="form" :model="record" label-width="165px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('策略名称')" prop="name">
          <el-input v-emoji v-model="record.name" ref="recordName" style="width:217px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('IPv6 地址前缀')" prop="addr">
          <el-input v-emoji v-model="record.addr" ref="addr" style="width:217px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('IPv6 前缀长度')" prop="len">
          <el-select v-model="record.len" ref="len" size='small' filterable style="width:217px;">
            <el-option v-for="item in prefixLenList" :key="item" :label="item" :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('源IPv4地址池')" prop="pool">
          <el-input v-emoji v-model="record.pool" ref="pool" style="width:217px;" size='small' :placeholder="$t('例如:172.18.202.0/24')"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addInfo, editInfo } from '@/api/network/address/nat64'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'Nat64Dialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validatePrefixAddr = (rule, value, callback) => {
      let regexp = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!regexp.test(value)) {
        callback(new Error(this.$t('请输入正确的ip地址')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        id: null,
        old_name: '',
        name: '',
        addr: '',
        len: '96',
        pool: ''
      },
      recordCopy: {
        id: null,
        old_name: '',
        name: '',
        addr: '',
        len: '96',
        pool: ''
      },
      prefixLenList: ['32', '40', '48', '56', '64', '96'],
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }],
        pool: [{ required: false, validator: validateName, trigger: 'blur' }],
        addr: [{ required: false, validator: validatePrefixAddr, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['recordName', 'addr', 'pool']
        const selectData = ['len']
        selectClass(this.$refs, selectData)
        inputClass(this.$refs, data)
      })
      if (data) {
        this.record.id = data['id']
        this.record.name = data['name']
        this.record.old_name = data['name']
        this.record.addr = data['prefix-addr']
        this.record.len = data['prefix-len']
        this.record.pool = data['src-pool']
        // this.record = Object.assign(this.record, data)
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (!this.record.id) {
            addInfo(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('添加成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          } else {
            editInfo(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('编辑成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          }
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    }
  }
}
</script>
